package cc.ufinity.networkdiagnose.widget;

 
import java.util.List;

import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.manager.MessageWhat;
import cc.ufinity.networkdiagnose.manager.TelManager;
import cc.ufinity.networkdiagnose.manager.WlanManager;
import cc.ufinity.networkdiagnose.view.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.telephony.NeighboringCellInfo;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TD_GSM_RssiWidget extends GeneralTestWidget {

	private Handler handler;
	private Activity ctx;
	private TelManager tm;
	private WlanManager wlanManager;
	private LinearLayout phoneLayout;
	private Button btnBack;
	private TextView headTitle;
	private TextView neighbor;
	private List<NeighboringCellInfo> neighborInfos;

	public TD_GSM_RssiWidget(Activity context) {
		super(context);
		inflate(context, R.layout.td_gsm_rssi_widget, this);
		this.ctx = context;
		btnBack = (Button) this.findViewById(R.id.rssi_btnback);
		headTitle=(TextView)findViewById(R.id.head_title33);
//		neighbor = (TextView)findViewById(R.id.neighbor_rssi);
		wlanManager = new WlanManager(ctx);
		phoneLayout = (LinearLayout) this.findViewById(R.id.myPhoneInfo);
		
		btnBack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
	               // back(ctx);
	            	switch(GlobalApp.nowNetType) {
					case 1:
						backToTestWidget_Wlan(ctx);
						break;
					case 2:
						backToTestWidget_TD(ctx);
						break;
					case 3:
						backToTestWidget_GSM(ctx);
						break;
					default:
						break;
					}
	            }
		});
		handler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case MessageWhat.TEL_STATE_SUCCESS:
					updateTel();    
					break;
				}
			}
		};

		tm = new TelManager(ctx);
		neighborInfos=tm.getNeighborInfos();
		tm.setHandler(handler);
		wlanManager.setHandler(handler);
		 
	}
 
	 
	private void updateTel() {
	 	phoneLayout.removeAllViews();
		TextView tv = new TextView(ctx);
		LayoutParams lp = new LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		lp.setMargins(15, 5, 5, 5);
		tv.setLayoutParams(lp);
		if(tm.getNetworkType().equals("EDGE") || tm.getNetworkType().equals("GPRS")){
			headTitle.setText("GSM");
		} else if(tm.getNetworkType().equals("HSDPA") || tm.getNetworkType().equals("HSUPA")){
			headTitle.setText("TD");
		}
		tv.setText("网络类型: " + tm.getNetworkType());
		tv.setTextColor(Color.BLACK);
		phoneLayout.addView(tv);
		tv = new TextView(ctx);
		tv.setLayoutParams(lp);
		tv.setTextColor(Color.BLACK);
		tm.getStrSignalStrength();
//		tv.setText("BER: " + tm.getBER() + "\n信号强度: " + tm.getStrSignalStrength()+" dBm");
		System.out.println("BER_______"+tm.getBER());
		tv.setText("信号强度: " + tm.getStrSignalStrength()+" dBm");
		phoneLayout.addView(tv);
		tv=new TextView(ctx);
		tv.setLayoutParams(lp);
		tv.setText("LAC: " + tm.getLac() + "\nCI: " + tm.getCI());
		tv.setTextColor(Color.BLACK);
		phoneLayout.addView(tv);

		StringBuffer sb = new StringBuffer();
		
//		if(neighborInfos!=null && neighborInfos.size()>0){
//			System.out.println("-----相邻小区   " + neighborInfos.size());
//			for(int i=0;i<neighborInfos.size();i++){	
//				if(i == 0) {
//					sb.append("LAC: " + neighborInfos.get(i).getLac() + "\nCI: " + neighborInfos.get(i).getCid());
//				} else {
//					sb.append("\n\nLAC: " + neighborInfos.get(i).getLac() + "\nCI: " + neighborInfos.get(i).getCid());
//				}
//				
//			}
//		} else {
//			sb.append("未找到相邻的小区LAC和CI");
//		}
//		
//		neighbor.setText(sb.toString());
	}
}
